<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="60%" @close="resetForm">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="所属标签" >
        <el-input v-model="formData.labelName" disabled/>
      </el-form-item>
      <el-form-item label="标签规则" prop="rule">
        <el-input type="textarea" :rows="7" v-model="formData.rule"/>
      </el-form-item>
      <el-form-item label="连接符" >
        <el-tooltip
          class="box-item"
          effect="dark"
          content="或运算符：A+B 表示满足A或者B"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('+')"> + </el-button>
        </el-tooltip>
        <el-tooltip
          class="box-item"
          effect="dark"
          content="非运算符：A-B 表示满足A且不满足B"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('－')"> － </el-button>
        </el-tooltip>

        <el-tooltip
          class="box-item"
          effect="dark"
          content="与运算符：A*B 表示同时满足A和B"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('*')"> * </el-button>
        </el-tooltip>

        <el-tooltip
          class="box-item"
          effect="dark"
          content="左括号：调整运算符之间的执行顺序"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('(')"> ( </el-button>
        </el-tooltip>

        <el-tooltip
          class="box-item"
          effect="dark"
          content="右括号：调整运算符之间的执行顺序"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle(')')"> ) </el-button>
        </el-tooltip>
        <el-tooltip
          class="box-item"
          effect="dark"
          content="等号：正文=A 表示满足正文字段出现A"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('=')"> = </el-button>
        </el-tooltip>
        <el-tooltip
          class="box-item"
          effect="dark"
          content="通配符：中？人表示两字之间可以出现一个随机字符，比如中国人"
          placement="bottom-start"
        >
          <el-button type="primary" @click="handle('?')"> ? </el-button>
        </el-tooltip>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as LabelApi from "@/api/label";

defineOptions({name: 'RuleForm'})

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formRef = ref() // 表单 Ref

const formData = ref({
  id: 0,
  labelId: undefined,
  rule: undefined,
  remark: undefined,
  sort: 0
})
const formRules = reactive({
  rule: [{required: true, message: '标签规则不能为空', trigger: 'blur'}]
})

/** 打开弹窗 */
const open = async (type: 'create' | 'update', node, id:number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type) + '规则'
  formType.value = type
  // 修改时，设置数据
  if (id) {
    formData.value.id = id
    formData.value = await LabelApi.selectRuleOne(id)
    formData.value.labelName = node.name
  }else{
    formData.value.labelId = node.id
    formData.value.labelName = node.name
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await LabelApi.createRule(formData.value)
    } else {
      await LabelApi.updateRule(formData.value)
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/** 选择操作符操作 + ( ) = */
const handle = (caoZuoFu: string) => {
  formData.value.rule = formData.value.rule + caoZuoFu
}
const resetForm = () =>{
  formData.value = {
    id: 0,
    labelId: undefined,
    rule: undefined,
    remark: undefined,
    sort: 0
  }
}
</script>

<style scoped>

</style>
